package org.fin.framework.ioc.config;

import org.fin.framework.ioc.factory.AnnotationApplicationContext;
import org.fin.framework.ioc.util.ApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextLoaderListener implements ServletContextListener {

  public final String CONFIG_CLASS = "configClass";


  @Override
  public void contextInitialized(ServletContextEvent servletContextEvent) {


    ServletContext sctx = servletContextEvent.getServletContext();

    String className = sctx.getInitParameter(CONFIG_CLASS);

    try {
      Class<?> configCls = Class.forName(className);
      AnnotationApplicationContext ctx = new AnnotationApplicationContext(configCls);
      ApplicationContextUtils.setApplicationContext(ctx);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }

  }

  @Override
  public void contextDestroyed(ServletContextEvent servletContextEvent) {

  }
}
